import config
import json
from modules.ApiClient import ApiClient,ApiClientError


class TaskApi(ApiClient):
    api_headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'}

    def __init__(self):
        super().__init__(config.REMOTE_TASK_URI)

    def get_tasks(self, params=None):
        if not params:
            params = {}
        task_params = {'client_uuid': config.CLIENT_UUID}
        task_params.update(params)
        remote_tasks = self.json_get('/api/remote_downloads/tasks', body=task_params, headers=self.api_headers)
        return remote_tasks

    def report(self, params=None):
        if not params:
            params = {}
        task_params = {}
        task_params.update(params)
        re = self.json_post('/api/remote_downloads/task_report', body=task_params, headers=self.api_headers)
        return re

    def json_get(self, path='/', body=None, headers={}):
        data = super().get(path, body, headers)
        if data:
            data = json.loads(data)

        return data

    def json_post(self, path='/', body=None, headers={}):
        data = super().post(path, body, headers)
        if data:
            data = json.loads(data)

        return data

